package com.scy.demo.interceptor;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 司晨雨
 * @date 2020/6/9 21:58
 * @description
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws IOException {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            HttpSession session = request.getSession();
            if(StringUtils.isBlank(username)){
                username = session.getAttribute("username").toString().trim();
            }
            if(StringUtils.isBlank(password)){
                password = session.getAttribute("password").toString().trim();
            }
            if("scy".equals(username) && "scy".equals(password)){
                session.setAttribute("username", "scy");
                session.setAttribute("password", "scy");
                return true;
            }else {
                response.sendRedirect("/login");
            }
        return false;

    }




}
